package toanstt.com.gameplay;

import toanstt.com.MatchUp.MainActivity;
import toanstt.com.MatchUp.SCR;
import toanstt.com.MatchUp.T;
import android.content.SharedPreferences;

public class SaveGame 
{
	public  boolean saved;
	
	public void SaveGame()
	{
		SharedPreferences settings = MainActivity.activity.getSharedPreferences("toanCARD1", 0);
		SharedPreferences.Editor editor = settings.edit();
		String a="";
		for(int i =0;i<T.GAME_ROW;i++)
		{
			for(int j =0;j<T.GAME_COL;j++)
			{
				if(CellTable.CARD_SLIP_INDEX[i][j]==-1)
					a+='a';
				else
					a+= CellTable.CARDINDEX[i][j];
			}
		}

		editor.putString("GAME_INDEX", a);
		editor.putInt("GAME_I1", T.GAME_I1);
		editor.putInt("GAME_I2", T.GAME_I2);
		editor.putInt("GAME_ROW", T.GAME_ROW);
		editor.putInt("GAME_COL", T.GAME_COL);
		editor.putInt("GAME_FAIL", GameStatus.FAIL);
		editor.putInt("GAME_DONE", CellTable.COUNT_DONE);
		editor.putInt("GAME_NUMHINT", GameStatus.NUMHINT);
		
		editor.commit();
		
	}
	
	public boolean LoadGame()
	{
		T.IS_BUSY = true;
		T.GAME_COL =4;
		T.GAME_ROW =4;
		SharedPreferences settings = MainActivity.activity.getSharedPreferences("toanCARD1", 0);
		T.GAME_I1 = settings.getInt("GAME_I1", 0);
		T.GAME_I2 = settings.getInt("GAME_I2", 0);
		T.GAME_ROW = settings.getInt("GAME_ROW", 0);
		T.GAME_COL = settings.getInt("GAME_COL", 0);
		
		
	
		
		CellTable.CARDINDEX = new int[T.GAME_ROW][T.GAME_COL];
		
		String a;
		a = settings.getString("GAME_INDEX", null);
		int index =0;
		if(a!=null)
		{
			G.NewGame(T.GAME_I1, T.GAME_I2);
			GameStatus.FAIL =  settings.getInt("GAME_FAIL", 0);
			CellTable.COUNT_DONE = settings.getInt("GAME_DONE", 0);
			GameStatus.NUMHINT = settings.getInt("GAME_NUMHINT", 3);
			GameStatus.BUTTON.bitmap = GameStatus.BUTTON_BIT.bitmap[GameStatus.NUMHINT];
			for(int i =0;i<T.GAME_ROW;i++)
			{
				for(int j =0;j<T.GAME_COL;j++)
				{
					if(a.charAt(index)=='a')
					{
						//CellTable.CARDINDEX[i][j]=-1;
						CellTable.CARD_SLIP_INDEX[i][j] =-1;
					}
					else
						CellTable.CARDINDEX[i][j] = Integer.parseInt( (a.charAt(index) + ""));
					index++;
				}
			}
			
		}
		else
		{
			T.IS_BUSY = false;
			T.TOAST("You wasn't save any game");
			return false;
		}
		T.SCREEN = SCR.GAME;
		T.IS_BUSY = false;
		return true;
	}
	
}

